import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from sqlalchemy.orm import Session
from datetime import datetime, timedelta

from core.logger import logger
from common.utils import CommonTimeUtils

from app.celery_task.models.celery_task import CeleryTask
from app.service_quality.models.qos_traffic_statistics import QosTrafficStatistics
from app.notice_center.handlers.nc_n9e_alarm_record_handler import NCN9eAlarmRecordHandler
from app.notice_center.handlers.nc_log_alarm_record_handler import NCLogAlarmRecordHandler
from app.monitor_doris.handlers.monitor_doris_http_record_handler import MonitorDorisHttpRecordHandler
from app.celery_task.handlers.celery_task_handler import CeleryTaskHandler


class NCPrometheusMetricsHandler(object):

    def __init__(self):

        self.registry = CollectorRegistry(auto_describe=False)

        # "celery", "alarm_n9e", "alarm_log"
        self.devops_state = Gauge("devops_state", "devops platform sub business status. 1:ok, 0:failed",
                             ["sub_type"], registry=self.registry)
        
        self.nginx_qpm = Gauge("nginx_qpm", "gateway qpm value",
                                ["sub_type"], registry=self.registry)
        
        # 不带标签的用法
        # self.celery_state = Gauge(name="celery_state",
        #                    documentation="Celery is running. 1:ok, 0:failed",
        #                    registry=self.registry)
        # 带标签的用法
        # self.mq_messages = Gauge("mq_messages",
        #                      "Number of queue messages.",
        #                      ["cluster", "queue"], registry=self.registry)
        # self.mq_messages.labels(cluster=cluster, queue=queue_name).set(queue_info["messages_ready"])


    def get_all_metrics(self, db: Session):
        # 设置celery的工作状态
        celery_task_handler = CeleryTaskHandler()
        result = celery_task_handler.get_celery_state(db)
        if result:
            self.devops_state.labels(sub_type="celery").set(1)
        else:
            self.devops_state.labels(sub_type="celery").set(0)
        
            
        # 设置htt的接口状态采集任务的状态
        monitor_doris_http_record_handler = MonitorDorisHttpRecordHandler()
        result = monitor_doris_http_record_handler.get_http_record_state(db)
        if result:
            self.devops_state.labels(sub_type="http_code").set(1)
        else:
            self.devops_state.labels(sub_type="http_code").set(0)

        #设置日志告警的状态
        nc_log_alarm_record_handler = NCLogAlarmRecordHandler()
        result = nc_log_alarm_record_handler.get_nc_log_alarm_status(db)
        if result:
            self.devops_state.labels(sub_type="alarm_log").set(1)
        else:
            self.devops_state.labels(sub_type="alarm_log").set(0)
        
        
        # 设置n9e告警的工作状态
        nc_n9e_alarm_record_handler = NCN9eAlarmRecordHandler()
        result = nc_n9e_alarm_record_handler.get_nc_n9e_alarm_status(db)
        if result:
            self.devops_state.labels(sub_type="alarm_n9e").set(1)
        else:
            self.devops_state.labels(sub_type="alarm_n9e").set(0)
            
            
        # 获取nginx的qpm
        qpm_info = QosTrafficStatistics.get_item_last_value(db, "japi_qpm")
        if qpm_info:
            self.nginx_qpm.labels(sub_type="japi_qpm").set(qpm_info["value"])
        else:
            self.nginx_qpm.labels(sub_type="japi_qpm").set(0)
        
        
        # 返回prometheus格式的数据
        return prometheus_client.generate_latest(self.registry)



def main():
    from common.database import SessionLocal
    from pprint import pprint
    db = SessionLocal()
    prometheus_metrics = NCPrometheusMetricsHandler()
    queue_data = prometheus_metrics.get_all_metrics(db)
    pprint(queue_data)
    #pprint(METRICS_QUEUE)

if __name__ == "__main__":
    main()
